package day13;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;


import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

/**
 * @author oldliu
 * @since 1.0
 */
//SAX处理方式
public class Java82SaxReader extends DefaultHandler {
    @Override//文档开始
    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
        System.out.println(uri+",节点名:"+localName+",qName="+qName);
        System.out.println("属性：");
        for (int i = 0; i < attributes.getLength(); i++) {
            System.out.println(attributes.getQName(i)+":"+attributes.getValue(i));
        }

    }

    @Override
    public void endElement(String uri, String localName, String qName) throws SAXException {
        System.out.println("元素："+localName+"结束。"+qName);

        System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
    }

    @Override
    public void characters(char[] ch, int start, int length) throws SAXException {
        String str=new String(ch,start,length);
        System.out.println("value:"+str.trim());
    }

    @Override
    public void startDocument() throws SAXException {
        System.out.println("文档开始了");
    }

    @Override
    public void endDocument() throws SAXException {
        System.out.println("文档结束");
    }

    public static void main(String[] args) throws Exception {
        SAXParserFactory factory = SAXParserFactory.newInstance();
        SAXParser parser = factory.newSAXParser();
        parser.parse("file:///D:\\Documents\\桌面\\82\\3-java-advance\\9-xml-作业\\products.xml", new Java82SaxReader());

    }
}
